<?php

namespace Admin\Controller;

use Think\Controller;

class GuanggaoController extends PublicController
{
    private $model;

    /**
     *
     * 构造函数，用于导入外部文件和公共方法
     */
    public function _initialize()
    {
        $this->model = M('guanggao');
        $this->assign('modules', '轮播图管理');
    }

    /**
     * 轮播图列表
     */
    public function index()
    {
        $this->assign('title', '轮播图列表');
        $list = $this->model->order('sort=0, sort asc, id asc')->select();
        $this->assign('list', $list);
        $this->display();
    }

    /**
     *
     * 跳转添加或修改广告数据页面
     */
    public function add()
    {
        $list = [
            ['name' => '文艺在线', 'link' => '/pages/artistic_list/artistic_list'],
            ['name' => '文化日历', 'link' => '/pages/activity_list/activity_list'],
            ['name' => '积分兑换', 'link' => '/pages/Integral_mall/Integral_mall'],
            ['name' => '艺术社群', 'link' => '/pages/community_recruit/community_recruit'],
            ['name' => '文艺荟萃', 'link' => '/pages/artistic_works/artistic_works'],
            ['name' => '非遗专区', 'link' => '/pages/Intangible_culture_list/Intangible_culture_list'],
        ];

        $news = M("news")->field('id, title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();
        $event = M("event")->field('id, title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();
        $goods = M("goods")->field('id, name as title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();
        $group = M("group")->field('id, title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();
        $works = M("cultural_blend")->field('id, title')->where(['is_del' => 0,'review=1'])->order('id desc')->limit(0, 6)->select();
        $feiyi = M("Inheritance")->field('id, title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();

        $mapper = [
            'news'  => ['name' => '文艺在线', 'link' => '/pages/artistic_details/artistic_details?id='],
            'event' => ['name' => '文化日历', 'link' => '/pages/activity_details/activity_details?id='],
            'goods' => ['name' => '积分兑换', 'link' => '/pages/Integral_goodsDeatils/Integral_goodsDeatils?id='],
            'group' => ['name' => '艺术社群', 'link' => '/pages/recruit_details/recruit_details?id='],
            'feiyi' => ['name' => '非遗专区', 'link' => '/pages/Intangible_culture_detail/Intangible_culture_detail?id='],
            'works' => ['name' => '文艺荟萃', 'link' => '/pages/works_details/works_details?id='],
        ];

        foreach ($mapper as $key => $value) {
            foreach ($$key as $row) {
                $list[] = ['name' => $value['name'] . ' - ' . $row['title'], 'link' => $value['link'] . $row['id']];
            }
        }

        $this->assign("list", $list);

        //如果是修改，则查询对应广告信息
        if (intval($_GET['id'])) {
            $this->assign('title', '修改轮播图');
            $adv_id = intval($_GET['id']);
            $adv_info = $this->model->where('id=' . intval($adv_id))->find();
            if (!$adv_info) {
                $this->error('没有找到相关信息.');
            }
            $this->assign('adv_info', $adv_info);
        } else {
            $this->assign('title', '添加轮播图');
        }
        $this->display();
    }

    /**
     * 增加或更新轮播图
     */
    public function save()
    {
        $data['sort'] = isset($_POST['sort']) ? intval($_POST['sort']) : 0;
        $data['direct_to'] = isset($_POST['direct_to']) ? trim($_POST['direct_to']) : null;

        //上传图片
        if (!empty($_FILES["file"]["tmp_name"])) {
            $info = $this->upload_images($_FILES["file"], array('jpg', 'png', 'jpeg'), 'adv/' . date('Ymd'));
            if (!is_array($info)) {
                $this->error($info);
            } else {
                $data['photo'] = 'UploadFiles/' . $info['savepath'] . $info['savename'];
            }
        }

        //保存数据
        if (isset($_POST['id']) && intval($_POST['id'])) {
            $result = $this->model->where('id=' . intval($_POST['id']))->save($data);
        } else {
            $result = $this->model->add($data);
        }

        if ($result) {
            $this->success('操作成功.', 'index');
        } else {
            $this->error('操作失败.');
        }
    }

    public function del()
    {
        $adv_id = intval($_GET['did']);
        $check_info = $this->model->where('id=' . intval($adv_id))->find();
        if (!$check_info) {
            $this->error('系统繁忙，请时候再试！');
        }

        //修改对应的删除状态
        $up = $this->model->where('id=' . intval($adv_id))->delete();
        if ($up) {
            $url = "Data/" . $check_info['photo'];
            if (file_exists($url)) {
                @unlink($url);
            }
            $this->success('操作成功.', 'index');
        } else {
            $this->error('操作失败.');
        }
    }

    public function delPushIndex()
    {
        $id = intval($_GET['did']);
        $check_info = M('push_index')->where('id=' . $id)->find();
        if (!$check_info) {
            $this->error('操作失败.');
        }

        //修改对应的删除状态
        $up = M('push_index')->where('id=' . $id)->delete();
        if ($up) {
            $this->success('操作成功.', 'pushIndex');
        } else {
            $this->error('操作失败.');
        }
    }

    public function pushIndex()
    {
        $this->assign('title', '活动优选');
        $list = M('push_index')->order('sort=0, sort asc, id asc')->select();
        if (!empty($list)) {
            foreach ($list as $index => $row) {
                switch ($row['object_type']) {
                    case 1:
                        $list[$index]['object_type'] = '文艺在线';
                        $list[$index]['title'] = M("news")->where(['id' => $row['object_id']])->getField('title');
                        break;
                    case 2:
                        $list[$index]['object_type'] = '文化日历';
                        $list[$index]['title'] = M("event")->where(['id' => $row['object_id']])->getField('title');
                        break;
                    case 3:
                        $list[$index]['object_type'] = '积分商品';
                        $list[$index]['title'] = M("goods")->where(['id' => $row['object_id']])->getField('name');
                        break;
                    case 4:
                        $list[$index]['object_type'] = '艺术社群';
                        $list[$index]['title'] = M("group")->where(['id' => $row['object_id']])->getField('title');
                        break;
                    case 5:
                        $list[$index]['object_type'] = '非遗专区';
                        $list[$index]['title'] = M("inheritance")->where(['id' => $row['object_id']])->getField('title');
                        break;
                }
            }
        }
        $this->assign('list', $list);
        $this->display();
    }

    public function addPushIndex()
    {
        $this->assign('type', [
            1 => '文艺在线',
            2 => '文化日历',
            3 => '积分商品',
            4 => '艺术社群',
            5 => '非遗专区',
        ]);

        if (intval($_GET['id'])) {
            $info = M('push_index')->where('id=' . intval($_GET['id']))->find();
            $this->assign('info', $info);
            switch ($info['object_type']) {
                case 1:
                    $list = M("news")->field('id, title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();
                    break;
                case 2:
                    $list = M("event")->field('id, title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();
                    break;
                case 3:
                    $list = M("goods")->field('id, name as title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();
                    break;
                case 4:
                    $list = M("group")->field('id, title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();
                    break;
                case 5:
                    $list = M("inheritance")->field('id, title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();
                    break;
            }
            $this->assign('list', $list);
            $this->assign('title', '修改精选活动');
        } else {
            $this->assign('title', '添加精选活动');
        }
        $this->display();
    }

    /**
     * 保存信息
     */
    public function savePushIndex()
    {
        $data['sort'] = !empty($_POST['sort']) ? intval($_POST['sort']) : 0;
        $data['object_type'] = !empty($_POST['object_type']) ? intval($_POST['object_type']) : null;
        $data['object_id'] = !empty($_POST['object_id']) ? intval($_POST['object_id']) : null;
        if (empty($data['object_type']) || empty($data['object_id'])) {
            $this->error('操作失败.');
        }

        //保存数据
        if (isset($_POST['id']) && intval($_POST['id'])) {
            $result = M('push_index')->where('id=' . intval($_POST['id']))->save($data);
        } else {
            $result = M('push_index')->add($data);
        }

        if ($result) {
            $this->success('操作成功.', 'pushIndex');
        } else {
            $this->error('操作失败.');
        }
    }

    /**
     * 获取活动列表
     */
    public function ajaxGetObject()
    {
        $objectType = intval($_GET['type']);

        switch ($objectType) {
            case 1:
                $list = M("news")->field('id, title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();
                break;
            case 2:
                $list = M("event")->field('id, title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();
                break;
            case 3:
                $list = M("goods")->field('id, name as title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();
                break;
            case 4:
                $list = M("group")->field('id, title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();
                break;
            case 5:
                $list = M("inheritance")->field('id, title')->where(['is_del' => 0])->order('id desc')->limit(0, 6)->select();
                break;
        }

        exit(json_encode($list));
    }
}